//
//  UIImage+BSExtension.m
//  Baisi
//
//  Created by 刘家俊 on 17/3/27.
//  Copyright © 2017年 Andrew. All rights reserved.
//

#import "UIImage+BSExtension.h"

@implementation UIImage (BSExtension)

//这个系统的方法相对比较消耗性能，所以用上下图裁剪
//    self.imageListView.layer.cornerRadius = self.imageListView.bs_width * 0.5;
//    self.imageListView.layer.masksToBounds = YES;

- (instancetype)bs_circleImage
{
    //开启图形上下文
    UIGraphicsBeginImageContext(self.size);
    
    //上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //添加一个圆
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextAddEllipseInRect(ctx, rect);
    
    //裁剪
    CGContextClip(ctx);
    
    //绘制图片
    [self drawInRect:rect];
    
    //获得图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    return image;
}

+ (instancetype)bs_circleImageWith:(NSString *)name
{
    return [[self imageNamed:name] bs_circleImage];
}

@end
